const path = require('path')
const webpack = require("webpack")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const { CleanWebpackPlugin } = require("clean-webpack-plugin")


module.exports = (env,argv) =>{
  const config = {
    mode:"none",
    entry:"./src/index.js",
    output:{
      filename:"bundle.js",
      path:path.join(__dirname,'relece'),
      // publicPath:'dist/'
    },
    // devtool:'source-map',
    devtool:'eval',
    devServer:{
    //  hot:true,
     hotOnly:true,
     contentBase:"./public",
    //  proxy:{
    //    '/api':{
    //      //http:localhost:8080/api/users -> https://api.github.com/api/users
    //      target:'https://api.github.com',
    //      //http:localhost:8080/api/users -> https://api.github.com/users
    //      pathRewrite:{
    //        '^/api':''
    //      },
    //      //不能使用localhost:8080 作为请求 Github 的主机名
    //      changeOrigin:true
    //    }
    //  }
    },
    module:{
      rules:[
        {
          test:/.css$/,
          use:[
            'style-loader',
            'css-loader'
          ]
        },
        {
          test:/.png$/,
          use:{
            loader:'file-loader',
            options:{
              limit:10 * 1024
            }
          }
        },
        {
          test: /\.html$/,
          use: {
            loader: 'html-loader',
            options: {
              attributes:{
                list:[
                  {
                    tag: 'img',
                    attribute: 'src',
                    type: 'src',
                  },
                  {
                    tag: 'a',
                    attribute: 'href',
                    type: 'src',
                  }
                ]
              }
            }
          }
        }
      ]
    },
    plugins:[
      // new CleanWebpackPlugin(),
      //用于生成index.html文件的
      new HtmlWebpackPlugin({
        title:'webpack plugin',
        meta:{
          viewport:'width=device-width'
        }
      }),
      new CopyWebpackPlugin({
        patterns:[
          { from: 'public', to: './' },
        ]
      }),
      new webpack.DefinePlugin({
        API_BASE_URL:JSON.stringify('https://api.exampl')
      }),
      new webpack.HotModuleReplacementPlugin(),
    ]
  }
  if(env === 'production'){
    config.mode = 'production'
    config.devtool = false
    config.plugins = [
      ...config.plugins,
      new CleanWebpackPlugin()
    ]
  }
  return config
}

